﻿/*
XSettings Manager
Copyright (C) 2008 Matthew Perry

This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This libary/program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using BaseTools.XS;
using System.IO;
using System.Diagnostics;

namespace XSManager
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		private XSettings[] programs = XSettings.GetPrograms();
		
		void MainFormLoad(object sender, EventArgs e)
		{
			
			foreach(XSettings prog in programs)
			{
				string name = prog.ProgramInfo.Name;
				
				if (name.Trim().Length == 0)
					name = prog.Name;
				
				lstPrograms.Items.Add(name);
			}
		}
		
		void LoadProgram(XSettings xs)
		{
			XSInfo xsi = xs.ProgramInfo;
			
			lblProgramName.Text = xsi.Name;
			lblAuthor.Text = xsi.Author;
			txtDescription.Text = xsi.Description;
			
			picImage.Image = xs.ProgramImage;
			
			btnLaunch.Enabled = false;
			try
			{
				if (new FileInfo(xsi.Launch).Exists)
				{
					btnLaunch.Enabled = true;
				}
			}
			catch(Exception)
			{
			}
		}
		
		void LstProgramsSelectedIndexChanged(object sender, EventArgs e)
		{
			if (lstPrograms.SelectedIndex != -1)
			{
				LoadProgram(programs[lstPrograms.SelectedIndex]);
				btnDelete.Enabled = true;
				btnExplore.Enabled = true;
			}
			else
			{
				lblProgramName.Text = "";
				lblAuthor.Text = "";
				txtDescription.Text = "";
				
				picImage.Image = null;
				btnDelete.Enabled = false;
				btnLaunch.Enabled = false;
				btnExplore.Enabled = false;
			}
		}
		
		void BtnDeleteClick(object sender, EventArgs e)
		{
			if (lstPrograms.SelectedIndex != -1)
			{
				if (MessageBox.Show("Are you sure you want to delete this?", "Delete",
				                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
				{
					picImage.Image.Dispose();
					programs[lstPrograms.SelectedIndex].ProgramDirectory.Delete(true);
					lstPrograms.Items.Remove(lstPrograms.SelectedIndex);
					lstPrograms.SelectedIndex = -1;
				}
			}
		}
		
		void BtnLaunchClick(object sender, EventArgs e)
		{
			try
			{
				string pl = programs[lstPrograms.SelectedIndex].ProgramInfo.Launch;
				if (new FileInfo(pl).Exists)
				{
					Process p = new Process();
					p.StartInfo.WorkingDirectory = Path.GetDirectoryName(pl);
					p.StartInfo.FileName = pl;
					p.Start();
				}
			}
			catch(Exception)
			{
			}
		}
		
		void BtnExploreClick(object sender, EventArgs e)
		{
			Process.Start(new ProcessStartInfo("explorer.exe", "\"" + XSettings.SettingsFolder + "\\" +
			                                   programs[lstPrograms.SelectedIndex].Name + "\""));
		}
	}
}
